/*
Content: Table G.4
Paper: "Immigrants Return Intentions and Labor Market Behavior when the Home Country is Unsafe"
Journal: Journal of Labor Economics
Authors: Jacopo Bassetto, Teresa Freitas-Monteiro
*/

********************************************************************************
**# LOAD INTERMEDIATE ANALYSIS DATA
********************************************************************************

use "$intermediate/finaldata_main_ieb_noevent.dta" if match_date !=., clear


gen startdate = endepi
 
keep unemp_1m unemp_3m edu_max age frau  ysm_atunemp  ln_wage_any_lb ln_firmsize full_time_lb  ieb_beruf_kons_num_lb  share_highskill_lb dist_share_de_lb AKM_lb state woao_llm startdate startmy ieb_nat_id year month dist_alo_emp* endepi begepi entry_spell exit_spell match_date register_unemp


*** Run faster

keep if match_date != .
drop if year==.
keep if inrange(year,2000,2018)
keep if !inlist(ieb_nat_id,.,0)

********************************************************************************
**# LINK TERROR DATA ON THE STARTDATE OF UNEMPLOYMENT
********************************************************************************

merge m:1 startdate ieb_nat_id using "$globalterror\final/terror_dailydata_ieb_final.dta", gen(merge_rolling) keep(3) keepusing(atleast* cum*)


** Outcomes	**
replace register_unemp = 0 if match_date != . & register_unemp != 1 


	foreach x in 30 92  {
	gen register_`x'm = 1 if dist_alo_emp <= `x' 
	replace register_`x'm = 0 if dist_alo_emp > `x' 
	 
	}
	
	
cap drop register_1m register_3m
rename register_30m register_1m
rename register_92m register_3m

label var register_1m "Register within 1m"
label var register_3m "Register within 3m"

** Other controls **

replace edu_max=1 if edu_max==0 // =0 is missing
	
g ysm_v2= int((endepi-entry_spell)/365)
rename ysm_atunemp ysm_old
rename ysm_v2 ysm_atunemp
label var ysm_atunemp "Years since mig."

* Take log of the number of events*

* TREATMENT
foreach var in   terror     {
foreach day in 90 30 15 {
local start=`day'+1
g ln_cum_`var'_`day'd=ln(cum_`var'_`day'd+1)
g ln_cum_`var'_`day'd_0=ln(cum_`var'_`day'd_0+1)
}
}

* LAGS
foreach var in   terror     {
foreach day in 365 {
foreach t in 90 30 15 {
g ln_cum_`var'_`day'l_`t's=ln(cum_`var'_`day'l_90s+1)
g cum_`var'_`day'l_`t's_d=(cum_`var'_`day'l_90s>0)
replace cum_`var'_`day'l_`t's_d=. if cum_`var'_`day'l_90s==.
}
}
}


	
	
* KEEP ONLY RELEVANT OBSERVATIONS
* Indicator for non missing on control variables
g allcontrols=1 if  unemp_3m!=. & edu_max!=. & age !=. & frau!=. & ysm_atunemp!=. & ln_wage_any_lb!=. & ln_firmsize!=. & full_time_lb!=. & ieb_beruf_kons_num_lb!=. & share_highskill_lb!=. & dist_share_de_lb!=. & AKM_lb!=. & state!=. & woao_llm!=.


keep if year >= 2000 & allcontrols ==1 & register_unemp == 1

********************************************************************************
**# TABLE G.4
********************************************************************************

est clear 
foreach out in unemp_3m {
	
	foreach day in 90 {	
		
global fe "i.ieb_nat_id#i.year i.state i.year#i.month" 
global controls " i.edu_max age frau c.ysm_atunemp##c.ysm_atunemp ln_wage_any_lb full_time_lb"
	
 reghdfe `out' i.atleast1terror_15d $controls c.ln_cum_terror_365l_15s if year >= 2000 & allcontrols ==1 & register_unemp == 1, vce(cluster ieb_nat_id) absorb($fe)
 eststo m1
 
 reghdfe `out' ln_cum_terror_15d $controls c.ln_cum_terror_365l_15s if year >= 2000 & allcontrols ==1 & register_unemp == 1, vce(cluster ieb_nat_id) absorb($fe)
 eststo m2
 
  reghdfe `out' i.atleast1terror_30d $controls c.ln_cum_terror_365l_30s if year >= 2000 & allcontrols ==1 & register_unemp == 1, vce(cluster ieb_nat_id) absorb($fe)
 eststo m3
 
  
 reghdfe `out' ln_cum_terror_30d $controls c.ln_cum_terror_365l_30s if year >= 2000 & allcontrols ==1 & register_unemp == 1, vce(cluster ieb_nat_id) absorb($fe)
 eststo m4
 
 
 reghdfe `out' i.atleast1terror_90d $controls c.ln_cum_terror_365l_90s if year >= 2000 & allcontrols ==1 & register_unemp == 1, vce(cluster ieb_nat_id) absorb($fe)
 eststo m5
 
  
 reghdfe `out' ln_cum_terror_90d $controls c.ln_cum_terror_365l_90s if year >= 2000  & allcontrols ==1 & register_unemp == 1, vce(cluster ieb_nat_id) absorb($fe)
 eststo m6
	}
  esttab m* using "$table/tableG4_final.tex", replace tex star(* 0.10 ** 0.05 *** 0.01) se(3) b(4) ///
    keep(*atleast* ln_cum*) nocons nobase label nonotes ///
    mgroups("Emp. within 3 months", pattern(1 0 0 0 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
    mtitles("15 days" " " "1 month" " " "3 months" " ") ///
    coeflabel(1.atleast1terror_15d "Terrorist attacks > 0" ln_cum_terror_15d "Terrorist attacks (log)" ///
              1.atleast1terror_30d "Terrorist attacks > 0" ln_cum_terror_30d "Terrorist attacks (log)" ///
              1.atleast1terror_90d "Terrorist attacks > 0" ln_cum_terror_90d "Terrorist attacks (log)") ///
    scalars("N Observations" "ymean Mean outcome") ///
    sfmt(%9.0fc %9.3f) ///
    addnote("Origin country FE x Year FE: Yes" "Year FE x Month FE: Yes" "State of residency FE: Yes" ///
            "Individual controls: Yes" "Extended controls: Yes" "Lagged terrorist attacks: Yes") ///
    fragment
}


		
		